const mongoose = require('mongoose')
const Group = mongoose.model('Group')
const Sprint = mongoose.model('Sprint')
const config = require('../common/config')
// const Log = mongoose.model('Log')
// const request = require('request')
// const Session = require('../common/session')
// const config = require('../../config')
// const { registerName } = require('../common/utils')

// exports.Sprint = async (ctx, next) => {
//   let { page, group, Sprint, iterationPerPage = 8 } = ctx.request.query
//   let totalPage = Math.ceil(await Sprint.count({}) / iterationPerPage)
//   let IterationList = await Sprint.find({})
//     .skip((page - 1) * iterationPerPage)
//     .limit(iterationPerPage)
//     .exec()
// }
exports.deleteGroup = async ctx => {
  const { groupId } = ctx.request.body
  try {
    let group = await Group.findOne({_id: groupId}).exec()
    if (group) {
      group.group_status = 0
      await group.save()
      ctx.body = 1
    } else {
      ctx.body = 0
    }
  } catch (e) {
    ctx.body = 0
  }
}

exports.modifySprint = async ctx => {
  const { sprintId, sprintName, sprintStatus } = ctx.request.body
  try {
    let sprint = await Sprint.findOne({_id: sprintId}).exec()
    if (sprint) {
      sprint.sprint_name = sprintName
      sprint.sprint_status = sprintStatus
      await sprint.save()
      ctx.body = 1
    } else {
      ctx.body = 0
    }
  } catch (e) {
    ctx.body = 0
  }
}
exports.addGroup = async ctx => {
  const { groupName } = ctx.request.body
  try {
    await Group.create({
      group_name: groupName
    })
    ctx.body = config.SUCCESS
  } catch (e) {
    ctx.body = config.ERROR
  }
}

exports.getGroup = async ctx => {
  let groupList = await Group.find({}).exec()
  ctx.body = groupList.map(item => ({
    groupId: item._id,
    groupName: item.group_name,
    groupStatus: item.group_status
  }))
}

exports.getSprints = async ctx => {
  const { groupId, keyWords } = ctx.request.query
  let sprintList = await Sprint.find(groupId ? {group_id: groupId} : {}).exec()
  sprintList = sprintList.filter(item => item.sprint_name.indexOf(keyWords) > -1)
  ctx.body = sprintList.map(item => ({
    sprintId: item._id,
    sprintName: item.sprint_name,
    sprintStatus: item.sprint_status,
    sprintStartTime: item.sprint_starttime,
    sprintEndTime: item.sprint_endtime,
    sprintLastModifyTime: item.sprint_last_modify_time
  }))
}

exports.addSprint = async ctx => {
  const { sprintName, startStamp, endStamp } = ctx.request.body
  try {
    let sprint = await Sprint.findOne({sprint_name: sprintName})
    if (sprint) {
      ctx.body = config.ERROR
    } else {
      let sprint = await Sprint.create({
        sprint_name: sprintName,
        sprint_starttime: startStamp,
        sprint_endtime: endStamp
      })
      if (sprint) {
        ctx.body = config.SUCCESS
      } else {
        ctx.body = config.ERROR
      }
    }
  } catch (e) {
    console.log(e)
    ctx.body = config.ERROR
  }
}

// exports.getShopsList = async ctx => {
//   // property是筛选，可为shopId、userId、shopName，keyword为关键字
//   let { pageNum, property, keyword } = ctx.request.query
//   let totalPage = 1
//   let shopList = []
//   try {
//     if (!keyword) {
//       // 只有页数，不搜索
//       totalPage = Math.ceil(await Shop.count({}) / shopPerPage)
//       shopList = await Shop.find({})
//         .skip((pageNum - 1) * shopPerPage)
//         .limit(shopPerPage)
//         .exec()
//     } else if (!property) {
//       // 有keyword，没有筛选类型,需要每一种的查找
//       shopList = shopList.concat(await Shop.find({shop_name: new RegExp(keyword, 'igm')}))
//       shopList = shopList.concat(await Shop.find({user_id: keyword}))
//       shopList = shopList.concat(await Shop.find({_id: keyword}))
//       totalPage = Math.ceil(shopList.length / shopPerPage)
//       shopList = shopList.splice((pageNum - 1) * shopPerPage, shopPerPage)
//     } else {
//       // 有指定类型筛选
//       if (property === 'shopId') property = '_id'
//       if (property === 'userId') property = 'user_id'
//       if (property === 'shopName') property = 'shop_name'
//       shopList = shopList.concat(await Shop.find({[property]: new RegExp(keyword, 'igm')}))
//       totalPage = shopList.length
//       shopList = shopList.splice((pageNum - 1) * shopPerPage, shopPerPage)
//     }
//   } catch (err) {
//     console.log(err)
//   }
//   ctx.body = {
//     code: 1,
//     data: {
//       pageNum,
//       pages: totalPage,
//       list: shopList.map(item => ({
//         shopId: item._id,
//         userId: item.user_id,
//         shopName: item.shop_name,
//         shopPhone: item.shop_phone,
//         shopLocation: item.shop_location,
//         shopStatus: item.shop_status
//       }))
//     }
//   }
// }

// exports.getShopInfoById = async ctx => {
//   const { shopId } = ctx.params
//   try {
//     const shop = await Shop.findOne({_id: shopId}).exec()
//     ctx.body = {
//       code: 1,
//       data: {
//         shopId: shop._id,
//         userId: shop.user_id,
//         shopName: shop.shop_name,
//         shopLogo: shop.shop_logo,
//         shopAbstract: shop.shop_abstract,
//         shopLocation: shop.shop_location,
//         shopAnnouncement: shop.shop_announcement,
//         shopStatus: shop.shop_status, // 商铺状态
//         shopPhone: shop.shop_phone, // 商铺联系方式
//         shopWorkTime: shop.shop_work_time, // 商铺营业时间
//         shopDeliveryCost: shop.shop_delivery_cost, // 商铺配送费
//         shopStartDelivery: shop.shop_start_delivery, // 商铺起送价
//         shopStores_images: shop.shop_stores_images, // 商铺门店照
//         shopDetail_images: shop.shop_detail_images, // 商铺简介照
//         shopLongitude: shop.shop_longitude, // 商铺经度
//         shopLatitude: shop.shop_latitude // 商铺纬度
//       }
//     }
//   } catch (err) {
//     ctx.body = {
//       code: 1,
//       data: {}
//     }
//   }
// }

// exports.freezeShop = async ctx => {
//   const { shopId, type } = ctx.request.body
//   try {
//     let shop = await Shop.findOne({_id: shopId}).exec()
//     if (type === 'freeze') shop.shop_status = 3
//     if (type === 'unfreeze') shop.shop_status = 1
//     await shop.save()
//     ctx.body = {
//       code: 1
//     }
//   } catch (err) {
//     ctx.body = {
//       code: 0,
//       data: err
//     }
//   }
// }

// exports.getApplyList = async ctx => {
//   const { pageNum } = ctx.request.query
//   // 返回状态为未审核的申请
//   const totalPage = Math.ceil(await ShopApply.count({status: 0}) / shopApplyPerPage)
//   const applyList = await ShopApply.find({status: 0})
//     .skip((pageNum - 1) * shopApplyPerPage)
//     .limit(shopApplyPerPage)
//     .exec()
//   ctx.body = {
//     code: 1,
//     data: {
//       pageNum,
//       pages: totalPage,
//       list: applyList.map(item => ({
//         shopApplyId: item._id,
//         userId: item.user_id,
//         identificationNum: item.identification_num,
//         shopName: item.shop_name,
//         shopLocation: item.shop_location
//       }))
//     }
//   }
// }

// exports.getApplyInfoById = async ctx => {
//   const { shopApplyId } = ctx.params
//   let info
//   try {
//     info = await ShopApply.findOne({_id: shopApplyId}).exec()
//     ctx.body = {
//       code: 1,
//       data: {
//         shopApplyId: info._id,
//         userId: info.user_id,
//         identificationNum: info.identification_num,
//         identificationPic: info.identification_pic,
//         shopName: info.shop_name,
//         shopLogo: info.shop_logo,
//         shopAbstract: info.shop_abstract,
//         shopLocation: info.shop_location,
//         shopAuthImages: info.shop_auth_images
//       }
//     }
//   } catch (err) {
//     ctx.body = {
//       code: 0,
//       data: err
//     }
//   }
// }

// exports.auditApply = async ctx => {
//   // 1通过，2不通过
//   const { shopApplyId, result } = ctx.request.body
//   let apply
//   try {
//     apply = await ShopApply.findOne({_id: shopApplyId}).exec()
//     apply.status = result
//     // 通过就建shop表
//     if (+result === 1) {
//       Shop.create({
//         user_id: apply.user_id,
//         identification_num: apply.identification_num,
//         identification_pic: apply.identification_pic,
//         shop_name: apply.shop_name,
//         shop_logo: apply.shop_logo,
//         shop_abstract: apply.shop_abstract,
//         shop_location: apply.shop_location,
//         shop_stores_images: apply.shop_auth_images,
//         shop_type: apply.shop_type,
//         shop_longitude: apply.shop_longitude,
//         shop_latitude: apply.shop_latitude
//       })
//     }
//     await apply.save()
//     ctx.body = {
//       code: 1
//     }
//   } catch (err) {
//     ctx.body = {
//       code: 0,
//       data: err
//     }
//   }
// }

// exports.getShopTypeList = async ctx => {
//   const types = await ShopType.find({}).exec()
//   ctx.body = {
//     code: 1,
//     data: {
//       list: types.map(item => ({
//         shopTypeId: item._id,
//         typeDes: item.type
//       }))
//     }
//   }
// }

// exports.getFoodTypeList = async ctx => {
//   const types = await DishType.find({}).exec()
//   ctx.body = {
//     code: 1,
//     data: {
//       list: types.map(item => ({
//         foodTypeId: item._id,
//         typeDes: item.type
//       }))
//     }
//   }
// }

// exports.delShopType = async ctx => {
//   const { shopTypeId } = ctx.request.body
//   try {
//     await ShopType.remove({_id: shopTypeId})
//     ctx.body = {
//       code: 1
//     }
//   } catch (err) {
//     ctx.body = {
//       code: 0,
//       data: err
//     }
//   }
// }

// exports.delFoodType = async ctx => {
//   const { foodTypeId } = ctx.request.body
//   try {
//     await DishType.remove({_id: foodTypeId})
//     ctx.body = {
//       code: 1
//     }
//   } catch (err) {
//     ctx.body = {
//       code: 0,
//       data: err
//     }
//     console.log(err)
//   }
// }

// exports.addFoodType = async ctx => {
//   const { typeDes } = ctx.request.body
//   try {
//     await DishType.create({typeDes})
//     ctx.body = {
//       code: 1
//     }
//   } catch (err) {
//     ctx.body = {
//       code: 0,
//       data: err
//     }
//     console.log(err)
//   }
// }

// exports.addShopType = async ctx => {
//   const { typeDes } = ctx.request.body
//   try {
//     await ShopType.create({typeDes})
//     ctx.body = {
//       code: 1
//     }
//   } catch (err) {
//     ctx.body = {
//       code: 0,
//       data: err
//     }
//     console.log(err)
//   }
// }

// exports.getUserList = async ctx => {
//   // property是筛选，可为userName、userId、phone, keyword为关键字
//   let { pageNum, property, keyword } = ctx.request.query
//   let totalPage = 1
//   let userList = []
//   try {
//     if (!keyword) {
//       // 只有页数，不搜索
//       totalPage = Math.ceil(await User.count({}) / userPerPage)
//       userList = await User.find({})
//         .skip((pageNum - 1) * userPerPage)
//         .limit(userPerPage)
//         .exec()
//     } else if (!property) {
//       // 有keyword，没有筛选类型,需要每一种的查找
//       userList = userList.concat(await User.find({user_name: new RegExp(keyword, 'igm')}))
//       userList = userList.concat(await User.find({_id: keyword}))
//       if (Number.isInteger(+keyword)) {
//         userList = userList.concat(await User.find({phone_num: +keyword}))
//       }
//       totalPage = Math.ceil(userList.length / userPerPage)
//       userList = userList.splice((pageNum - 1) * userPerPage, userPerPage)
//     } else {
//       // 有指定类型筛选,注意数字值要先判断为数字才查找
//       if (property === 'phone' && Number.isInteger(+keyword)) {
//         property = 'phone_num'
//         userList = userList.concat(await User.find({[property]: +keyword}).exec())
//       } else if (property === 'userName') {
//         property = 'user_name'
//         userList = userList.concat(await User.find({[property]: new RegExp(keyword, 'igm')}).exec())
//       } else if (property === 'userId') {
//         property = '_id'
//         userList = userList.concat(await User.find({[property]: keyword}).exec())
//       }
//       totalPage = Math.ceil(userList.length / userPerPage)
//       userList = userList.splice((pageNum - 1) * userPerPage, userPerPage)
//     }
//   } catch (err) {
//     console.log(err)
//     // 当搜索失败，返回空数据
//   }
//   ctx.body = {
//     code: 1,
//     data: {
//       pageNum,
//       pages: totalPage,
//       list: userList.map(item => ({
//         userId: item._id,
//         userName: item.user_name,
//         phone: item.phone_num,
//         address: item.accept_address,
//         avator: item.avator,
//         introduction: item.introduction,
//         gender: item.gender
//       }))
//     }
//   }
// }
